import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'home',
    component:HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'),
    redirect:"/User/cart",
    children:[
      {path:'cart',component:()=> import('../components/user/Cart.vue')}
    ]
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "order" */ '../views/order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "free" */ '../views/free.vue')
  },
  {
    path: '/Detail',
    name: 'Detail',
    component: () => import(/* webpackChunkName: "Detail" */ '../views/Detail.vue')
  },
  {
    path: '/*',
    component: () => import(/* webpackChunkName: "Error" */ '../views/Error.vue')
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})



// 全局前置守卫
// router.beforeEach((to,from,next)=>{
//   if(to.path == '/user'){
//     let token = localStorage.getItem('x-auth-token');
//     if(token){
//       next()
//     }else{
//       // 提示登录
//       console.log('请登录')
//       store.dispatch("showToast/asyncIsShowToast",{
//         show:true,
//         msg:"请先登录",
//         type:'danger'
//       })
//     }
//   }else{
//     next(); // 其他组件正常显示
//   }
// })




// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};
export default router
